package com.nsl.my_youlai.bbu2.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsl.my_youlai.bbu2.convert.apparatus_room.ApparatusRoomConverter;
import com.nsl.my_youlai.bbu2.convert.apparatus_room.ApparatusRoomMapStructConverter;
import com.nsl.my_youlai.bbu2.model.entity.ApparatusRoomEntity;
import com.nsl.my_youlai.bbu2.model.bo.ApparatusRoomBo;
import com.nsl.my_youlai.bbu2.model.query.ApparatusRoomPageQuery;
import com.nsl.my_youlai.bbu2.model.vo.ApparatusRoomVoExcelString;
import com.nsl.my_youlai.bbu2.service.ApparatusRoomService;
import com.nsl.my_youlai.common.result.UploadFileStatus;
import com.nsl.my_youlai.config.props.BbuReturnProps;
import com.nsl.my_youlai.mapper.ApparatusRoomMapper;
import com.nsl.my_youlai.mystar.NslUtilDateTime;
import com.nsl.my_youlai.mystar.NslUtilFilePath;
import com.nsl.my_youlai.mystar.easy_excel.NslUtilEasyExcel;
import com.nsl.my_youlai.mystar.easy_excel.check.NslUtilExcelValid;
import com.nsl.my_youlai.mystar.easy_excel.model.ExcelReadCheck;
import com.nsl.my_youlai.mystar.easy_excel.model.ExcelReadResult;
import com.nsl.my_youlai.mystar.enums.NslEnumMime;
import com.nsl.my_youlai.mystar.enums.NslEnumTransformException;
import com.nsl.my_youlai.mystar.enums.NslEnumUploadStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ApparatusRoomServiceImpl extends ServiceImpl<ApparatusRoomMapper, ApparatusRoomEntity> implements ApparatusRoomService {

    private final ApparatusRoomMapper apparatusRoomMapper;

    private final BbuReturnProps bbuReturnProps;


    @Override
    public ExcelReadResult<ApparatusRoomVoExcelString> readApparatusRoomVoStringFromExcel(String excelFilePath, String sheetName){
        ExcelReadResult<ApparatusRoomVoExcelString> readResult= NslUtilEasyExcel.readExcelSheet2StringList(excelFilePath,sheetName, ApparatusRoomVoExcelString.class);

        List<ExcelReadCheck> checkList=readResult.getCheckList();
        List<ApparatusRoomVoExcelString> voList=readResult.getRecordList();

        NslUtilExcelValid.validList(voList,checkList);

        return readResult;
    }


    @Override
    public List<ApparatusRoomBo> voList2BoList(List<ApparatusRoomVoExcelString> voList) throws NslEnumTransformException {
        return ApparatusRoomConverter.toBoList(voList);
    }

    @Override
    public Page<ApparatusRoomEntity> getPage(ApparatusRoomPageQuery queryParams) {
        return null;
    }

    @Override
    public UploadFileStatus importApparatusRoomlExcel(MultipartFile uploadFile) throws NslEnumTransformException {
        String originalFileName=uploadFile.getOriginalFilename();
        String fileExtension= NslUtilFilePath.getFileExtension(originalFileName);

        UploadFileStatus uploadFileStatus =new UploadFileStatus();
        uploadFileStatus.setOriginalFileName(originalFileName);
        List<String> msgList= uploadFileStatus.getMsgList();
        if(!fileExtension.equalsIgnoreCase(NslEnumMime.OFFICE_XLSX.getExtension())){
            uploadFileStatus.setUploadStatusEnum(NslEnumUploadStatus.SAVE_FAIL);
            msgList.add("上传文件必须为*.xlsx类型");
            return uploadFileStatus;
        }

//        String uuid= NslUtilUuid.generateUuidWithOutHyphen();
        String dataTimePath=bbuReturnProps.getApparatusRoomExcelUploadDirPath()+ NslUtilDateTime.getTimeStamp()+".xlsx";
        String saveFilePath=NslUtilFilePath.buildFilePathWithUuidAtRear(dataTimePath);


        log.info("saveFilePath = {}",saveFilePath);
        File saveFile=new File(saveFilePath);

        try {
            uploadFile.transferTo(saveFile);
            uploadFileStatus.setUploadStatusEnum(NslEnumUploadStatus.SAVE_SUCCESS);

        } catch (IOException e) {
            uploadFileStatus.setUploadStatusEnum(NslEnumUploadStatus.SAVE_FAIL);
            msgList.add("服务器内容部错误");

            //todo:加入错误日志
            return uploadFileStatus;
        }

        ExcelReadResult roomReadResult =readApparatusRoomVoStringFromExcel(saveFilePath,bbuReturnProps.getApparatusRoomSheetName());

        List<ApparatusRoomVoExcelString> voList=roomReadResult.getRecordList();
        List<ApparatusRoomBo> boList=voList2BoList(voList);

        List<ApparatusRoomEntity> entityList= ApparatusRoomMapStructConverter.INSTANCE.boList2EntityList(boList);
        uploadFileStatus.setUploadStatusEnum(NslEnumUploadStatus.UPLOAD_SUCCESS_EVERY_THINK_OK);
        uploadFileStatus.getMsgList().add(boList.size()+"");

        this.saveBatch(entityList);

        //到了这里，文件已经上传成功，下面就是读文件，保存操作
        return uploadFileStatus;
    }

    public Map<String,ApparatusRoomBo> voList2BoMap(List<ApparatusRoomVoExcelString> voList) throws NslEnumTransformException {
        List<ApparatusRoomBo> boList=ApparatusRoomConverter.toBoList(voList);
        Map<String,ApparatusRoomBo> roomBoMap=new HashMap<>();
        for (ApparatusRoomBo bo : boList) {
            roomBoMap.put(bo.getId()+"",bo);
        }
        return roomBoMap;
    }

}
